package cn.com.victorysoft.xqfx.action;

import cn.com.victorysoft.eaf.authority.exposure.AppUser;
import cn.com.victorysoft.eaf.util.JSONUtil;
import cn.com.victorysoft.eaf.util.TreeUtil;
import cn.com.victorysoft.oadd.usrmgr.Department;
import cn.com.victorysoft.oadd.usrmgr.UserManage;
import cn.com.victorysoft.oadd.util.DateUtil;
import cn.com.victorysoft.oadd.util.NumberUtil;
import cn.com.victorysoft.oadd.util.StringUtil;
import cn.com.victorysoft.BaseAction;
import cn.com.victorysoft.xqfx.bean.XqfxBean;
import cn.com.victorysoft.xqfx.dao.XqfxDAO;
import com.opensymphony.xwork2.ActionContext;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import static org.apache.struts2.ServletActionContext.getRequest;
import static org.apache.struts2.ServletActionContext.getResponse;

/**
 * Created by adagio on 16/3/21.
 *
 */
/**
 * @author WANGNN
 *
 */
public class XqfxAction extends BaseAction {



    //注入标准管理bean
    private XqfxBean xqfxBean = new XqfxBean();
    //注入标准管理dao
    private XqfxDAO xqfxDao;

    /**
     * @author zhaoguangyong
     *  获取一级节点
     * @throws IOException
     *
     * */
    public String init() throws IOException {
        HttpServletRequest request = getRequest();
        HttpServletResponse response = getResponse();

        //查询一级节点数据
        List list = this.xqfxDao.getTreeList();
        //返回数据list
        ArrayList result_list = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            HashMap map = (HashMap) list.get(i);
            HashMap map_ = new HashMap();
            map_.put("id", map.get("BZFL_ID"));
            map_.put("text", map.get("BZFLMC")); //节点名称
            map_.put("pid", "0");//树默认根节点id为"0",一个隐藏节点。
            map_.put("created", map.get("BZFLSM"));//节点附加数据1
            map_.put("book", map.get("ORDER_NUMBER"));//节点附加数据2
            map_.put("tjid", map.get("TJID"));//节点附加数据3
            result_list.add(map_);
        }
        response.setContentType("application/x-json; charset=utf-8");
        response.setHeader("pragma", "no-cache");
        response.setHeader("cache-control", "no-cache");
        response.getWriter().print(
                TreeUtil.getTreeJSON(result_list, "0", "id", "text", "pid",
                        true, new String[] { "created", "book", "tjid"}, true));
        return NONE;
    }
    public String ywxqfxwh(){
        return SUCCESS;
    }
    //展开每个节点（点击节点前的"+"）响应的方法，即生成子节点
    public String open() throws IOException {
        HttpServletRequest request = getRequest();
        HttpServletResponse response = getResponse();

        //取得需要展开节点的ID  
        String id = request.getParameter("id");
        //查询子节点数据
        List list = this.xqfxDao.getTreeSonList(id);

        //生成展开节点的子节点  
        ArrayList result_list = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Map m = (Map) list.get(i);
            HashMap map = new HashMap();
            map.put("id", m.get("BZFL_ID"));
            map.put("text", m.get("BZFLMC"));
            map.put("pid", m.get("FBZFL_ID"));//树默认根节点id为"0",一个隐藏节点。  
            map.put("created", m.get("BZFLSM"));//节点附加数据1  
            map.put("book", m.get("ORDER_NUMBER"));//节点附加数据2  
            map.put("tjid", m.get("TJID"));//节点附加数据3
            result_list.add(map);
        }

        response.setContentType("application/x-json; charset=utf-8");
        response.setHeader("pragma", "no-cache");
        response.setHeader("cache-control", "no-cache");
        //将数据推向JSP页面  
        response.getWriter().print(
                TreeUtil.getTreeJSON(result_list, id, "id", "text", "pid",
                        true, new String[] { "created", "book","tjid" }, true));
        return NONE;
    }

    /**
     * 添加分类采集
     */
    public void saveXqfx() {
        Map result = new HashMap();
        HttpSession session = ServletActionContext.getRequest().getSession();
        AppUser user = (AppUser) session.getAttribute("_userInfo");
        if (user == null) {
            result.put("success", false);
            result.put("info", "请先登录");
            // 封装结果为json格式
            String temp = JSONUtil.object2json(result);
            this.backInfo(temp);
            return;
        }
        HttpServletRequest request = getRequest();
        //id如果是新增则是上级id，否则是id
        String id = request.getParameter("id").toString();
        String bzmc = request.getParameter("bzmc").toString();
        String bzms = request.getParameter("bzms").toString();
        String status = request.getParameter("status").toString();
        this.xqfxBean.setBzflmc(bzmc);
        this.xqfxBean.setBzflsm(bzms);

        boolean rs;
        //判断status ，修改数据
        if ("add".equals(status)) {
            this.xqfxBean.setFbzflId(id);
            //查询父节点的最大排序值
            List list = this.xqfxDao.getList(id);
            //1.根据id取到最大tjid 2.把tjid+1
            int s = 0;
            long tjid = 0;
            if (list.size() > 0) {
                Map map = (Map) list.get(0);
                String pxz = map.get("ORDER_NUMBER") + "";
                String tjid1 = map.get("TJID") + "";
                tjid = Long.parseLong(tjid1);
                s = Integer.parseInt(pxz);
            }
            //给排序值赋值
            if (s == 0) {
                this.xqfxBean.setOrderNumber(1);
            } else {
                this.xqfxBean.setOrderNumber(s + 1);
            }
            if(tjid == 0){
                List list1 = this.xqfxDao.getList1(id);
                String tjid11 = "";
                if(list1.size()>0){
                    tjid11 = ((Map)list1.get(0)).get("TJID")+"01";
                }else{
                    tjid11 = "1001";
                }
                this.xqfxBean.setTjid(tjid11);
            }else {
                this.xqfxBean.setTjid((tjid+1)+"");
            }
            rs = this.xqfxDao.addXqfx(xqfxBean);
        } else {
            this.xqfxBean.setBzflId(id);
            rs = this.xqfxDao.updateXqfx(xqfxBean);
        }
        result.put("success", rs);
        // 封装结果为json格式
        String temp = JSONUtil.object2json(result);
        this.backInfo(temp);
    }

    /**
     * 删除分类采集节点
     */
    public void deletXqfx() {
        Map result = new HashMap();
        HttpSession session = ServletActionContext.getRequest().getSession();
        AppUser user = (AppUser) session.getAttribute("_userInfo");
        if (user == null) {
            result.put("success", false);
            result.put("info", "请先登录");
            // 封装结果为json格式
            String temp = JSONUtil.object2json(result);
            this.backInfo(temp);
            return;
        }
        HttpServletRequest request = getRequest();
        String id = request.getParameter("id").toString();

        boolean rs = this.xqfxDao.deletXqfx(id);
        result.put("success", rs);
        // 封装结果为json格式
        String temp = JSONUtil.object2json(result);
        this.backInfo(temp);
    }

    //数节点上移
    public void MoveUp() {
        Map result = new HashMap();
        HttpSession session = ServletActionContext.getRequest().getSession();
        AppUser user = (AppUser) session.getAttribute("_userInfo");
        if (user == null) {
            result.put("success", false);
            result.put("info", "请先登录");
            // 封装结果为json格式
            String temp = JSONUtil.object2json(result);
            this.backInfo(temp);
            return;
        }
        HttpServletRequest request = getRequest();
        String id = request.getParameter("id").toString();
        String sjid = request.getParameter("sjid").toString();
        String pxz = request.getParameter("pxz").toString();
        //
        boolean re = xqfxDao.MoveUp(id, pxz, sjid);
    }

    //下移
    public void MoveDown() {
        Map result = new HashMap();
        HttpSession session = ServletActionContext.getRequest().getSession();
        AppUser user = (AppUser) session.getAttribute("_userInfo");
        if (user == null) {
            result.put("success", false);
            result.put("info", "请先登录");
            // 封装结果为json格式
            String temp = JSONUtil.object2json(result);
            this.backInfo(temp);
            return;
        }
        HttpServletRequest request = getRequest();
        String id = request.getParameter("id").toString();
        String sjid = request.getParameter("sjid").toString();
        String pxz = request.getParameter("pxz").toString();
        //
        boolean re = xqfxDao.MoveDown(id, pxz, sjid);
    }

    //左移右移逻辑相同执行相同方法
    public void Move_L_R() {
        Map result = new HashMap();
        HttpSession session = ServletActionContext.getRequest().getSession();
        AppUser user = (AppUser) session.getAttribute("_userInfo");
        if (user == null) {
            result.put("success", false);
            result.put("info", "请先登录");
            // 封装结果为json格式
            String temp = JSONUtil.object2json(result);
            this.backInfo(temp);
            return;
        }
        HttpServletRequest request = getRequest();
        String id = request.getParameter("id").toString();
        String sjid = request.getParameter("sjid").toString();
        //
        boolean re = xqfxDao.Move_L_R(id, sjid);
    }

    public XqfxBean getXqfxBean() {
        return xqfxBean;
    }

    public void setXqfxBean(XqfxBean xqfxBean) {
        this.xqfxBean = xqfxBean;
    }

    public XqfxDAO getXqfxDao() {
        return xqfxDao;
    }

    public void setXqfxDao(XqfxDAO xqfxDao) {
        this.xqfxDao = xqfxDao;
    }
}
